package com.chia.multienty.core.service.impl;

import com.chia.multienty.core.domain.condition.UserCondition;
import com.chia.multienty.core.domain.condition.WorkflowCondition;
import com.chia.multienty.core.pojo.ProcessBind;
import com.chia.multienty.core.mapper.ProcessBindMapper;
import com.chia.multienty.core.service.ProcessBindService;
import com.chia.multienty.core.mybatis.service.impl.KutaBaseServiceImpl;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chia.multienty.core.domain.dto.ProcessBindDTO;
import com.chia.multienty.core.parameter.user.ProcessBindDetailGetParameter;
import com.chia.multienty.core.parameter.user.ProcessBindPageGetParameter;
import com.chia.multienty.core.parameter.user.ProcessBindDeleteParameter;
import com.chia.multienty.core.parameter.user.ProcessBindSaveParameter;
import com.chia.multienty.core.parameter.user.ProcessBindUpdateParameter;
import com.github.yulichang.toolkit.MPJWrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import com.chia.multienty.core.domain.constants.MultientyConstants;
import com.chia.multienty.core.mybatis.MTLambdaWrapper;
import com.chia.multienty.core.util.ListUtil;
import com.chia.multienty.core.domain.enums.StatusEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chia.multienty.core.tools.IdWorkerProvider;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 工作流程绑定数据 服务实现类
 * </p>
 *
 * @author Multi Tenant Auto Generator
 * @since 2024-07-20
 */
@Service
@RequiredArgsConstructor
@Conditional(UserCondition.class)
public class ProcessBindServiceImpl extends KutaBaseServiceImpl<ProcessBindMapper, ProcessBind> implements ProcessBindService {
    @Override
    public ProcessBindDTO getDetail(ProcessBindDetailGetParameter parameter) {
        return selectJoinOne(ProcessBindDTO.class,
                        MPJWrappers.<ProcessBind>lambdaJoin().eq(ProcessBind::getBindId, parameter.getBindId()));
    }

    @Override
    public void update(ProcessBindUpdateParameter parameter) {
        ProcessBind processBind = new ProcessBind();
        BeanUtils.copyProperties(parameter, processBind);
        updateByIdTE(processBind);
    }

    @Override
    public void delete(ProcessBindDeleteParameter parameter) {
        removeByIdTE(parameter.getBindId());
    }

    @Override
    public IPage<ProcessBindDTO> getPage(ProcessBindPageGetParameter parameter) {
        return selectJoinListPage(parameter.getPageObj(), ProcessBindDTO.class,
                new MTLambdaWrapper<ProcessBind>()
                        .solveGenericParameters(parameter)
                        .in(!ListUtil.isEmpty(parameter.getBindIds()),
                                ProcessBind::getBindId,
                                parameter.getBindIds())
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ProcessBindSaveParameter parameter) {
        remove(
                mtLambdaWrapper()
                        .eq(ProcessBind::getProcessId, parameter.getProcessId())
                        .eq(ProcessBind::getArgId, parameter.getArgId())
        );
        List<ProcessBind> list = parameter.getMetaIds().stream().map(m ->
                new ProcessBind()
                        .setArgId(parameter.getArgId())
                        .setMetaId(m)
                        .setArgName(parameter.getArgName())
                        .setBindId(IdWorkerProvider.next())
                        .setMetaType(parameter.getMetaType())
                        .setProcessId(parameter.getProcessId())
        ).collect(Collectors.toList());

        saveBatch(list, list.size());
        parameter.setBindIds(list.stream().map(m->m.getBindId()).collect(Collectors.toList()));
    }


}
